<template>
  <div>
    <el-button type="primary" @click="addAlbum">+新增</el-button>
    <el-table :data="tableData" style="width: 100%" v-loading="tloading">
      <el-table-column prop="id" label="id" width="50" />
      <el-table-column prop="name" label="相册名称" width="200">
        <template slot-scope="scope">
          <el-input v-if="editObj.editIdx == scope.$index" v-model="editObj.name" />
          <span v-else>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="userId" label="研学风采类型" width="120">
        <template slot-scope="scope">
          {{ scope.row.userId == 0 ? "团队风采" : "个人风采" }}
        </template>
      </el-table-column>
      <el-table-column prop="showImg" label="相册封面">
        <template slot-scope="scope">
          <img :src="scope.row.showImg" alt="" style="height: 50px;" />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="相册创建时间" />
      <el-table-column prop="updateTime" label="相册更新时间" />
      <el-table-column prop="option" label="操作">
        <template slot-scope="scope">
          <template v-if="editObj.editIdx == scope.$index">
            <el-button type="text" @click="toEdit()" style="color: #85ce61;">确认修改</el-button>
            <el-button type="text" @click="editObj = { editIdx: -1 }" style="color: #a6a9ad">取消修改</el-button>
          </template>
          <template v-else>
            <el-button type="text" @click="editObj = { ...scope.row, editIdx: scope.$index }">相册编辑</el-button>
            <el-button type="text" @click="toOptions('upFileShow', scope.row)">素材上传</el-button>
            <el-button type="text" @click="toOptions('lookAlbumShow', scope.row)">查看素材</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="查看素材" width="40%" append-to-body :visible.sync="lookAlbumShow" class="lookAlbum">
      <lookalbum v-if="lookAlbumShow" :id="editObj.id" />
    </el-dialog>
    <el-dialog title="新增相册" width="24%" append-to-body :visible.sync="addShow" class="lookAlbum">
      <el-form ref="formData" size="small" label-width="80px">
        <el-form-item label="相册名称">
          <el-input v-model="editObj.name" placeholder="请输入相册名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="addShow = false">取消</el-button>
        <el-button :loading="tloading" type="primary" @click="addSubmit()">确认</el-button>
      </div>
    </el-dialog>
    <el-dialog title="素材上传" width="40%" append-to-body :visible.sync="upFileShow" @close="restForm()">
      <el-form ref="formData" :model="formData" :rules="rules" size="small" label-width="120px">
        <el-form-item label="素材类型" prop="type" style="width: 100%;">
          <el-select v-model="formData.type" placeholder="请选择" @change="(val) => changgeType(val)">
            <el-option label="图片" :value="0" />
            <el-option label="视频" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="素材上传" prop="imgPaths">
          <el-upload
            v-loading="coverImage_loading"
            class="avatar-uploader el-upload"
            v-for="(item, index) in formData.imgPaths"
            :key="index"
            :accept="accept"
            action="https://jsonplaceholder.typicode.com/posts/"
            :http-request="(params) => uploadDetailRequest(params, 'imgPaths')"
            :show-file-list="false"
          >
            <template v-if="item">
              <img v-if="formData.type == 0" :src="item" class="avatar" />
              <video v-else :src="item" class="avatar" />
            </template>
            <div v-else class="upload-text">选择图片</div>
            <div class="btn" v-if="item" @click.stop="deleteImg('imgPaths', item)">
              <el-button size="36" type="text" icon="el-icon-delete"></el-button>
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="restForm()">取消</el-button>
        <el-button :loading="upfile_loading" type="primary" @click="upFileSubmit()">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getProjectAlbumList, albumEdit, imgSubmit } from "@/api/mini/yanxue";
import { uploadFile } from "@/api/data";
export default {
  props: {
    id: {
      type: Number,
      default: 0,
    },
    projectid: {
      type: Number,
      default: 0,
    },
  },
  components: {
    lookalbum: () => import("./lookalbum"),
  },
  data() {
    return {
      tableData: [],
      tloading: false,
      editObj: { editIdx: -1 },
      upFileShow: false,
      formData: { type: 0, imgPaths: [""] },
      rules: {
        type: { required: true, message: "请输入图片类型", trigger: "blur" },
        imgPaths: { required: true, validator: this.validatePass2, trigger: "change" },
      },
      coverImage_loading: false,
      upfile_loading: false,
      accept: "image/*",
      lookAlbumShow: false,
      addShow: false,
    };
  },
  created() {
    this.getData();
  },
  methods: {
    addSubmit() {
      if (!!this.editObj.name) {
        this.toEdit();
      } else {
        this.$message.error("请输入相册名称");
      }
    },
    addAlbum() {
      this.addShow = true;
      this.editObj = { name: "" };
    },
    toOptions(key, row) {
      this[key] = true;
      this.editObj = row;
    },
    changgeType(val) {
      this.accept = val == 1 ? "video/*" : "image/*";
      this.formData.imgPaths = [""];
    },
    validatePass2(rule, value, callback) {
      let str = value.filter((x) => !!x).join(",");
      if (str == "") {
        callback(new Error("请选择素材"));
      } else {
        callback();
      }
    },
    upFileSubmit() {
      this.$refs.formData.validate((bol) => {
        if (bol) {
          let obj = {
            albumId: this.editObj.id,
            imgPaths: this.formData.imgPaths.filter((x) => !!x).join(","),
            type: this.formData.type,
          };
          this.upfile_loading = true;
          imgSubmit(obj).then((res) => {
            this.$message.success("上传成功");
            this.upfile_loading = false;
            this.restForm();
          });
        }
      });
    },
    restForm() {
      this.formData = { type: 0, imgPaths: [""] };
      this.upFileShow = false;
      this.accept = "image/*";
      this.$refs["formData"].resetFields();
    },
    uploadDetailRequest(params, key) {
      this.coverImage_loading = true;
      uploadFile(params).then((res) => {
        this.formData[key].unshift(res.data);
        this.coverImage_loading = false;
        this.$refs.formData.validateField("imgPaths");
      });
    },
    deleteImg(key, url) {
      this.formData[key] = this.formData[key].filter((x) => x != url);
    },
    toEdit() {
      let obj = {
        id: this.editObj.id || void 0,
        bingInfoId: this.id,
        name: this.editObj.name,
        projectId: this.projectid,
        type: this.id == 0 ? 0 : 1,
      };
      this.tloading = true;
      albumEdit(obj).then((res) => {
        this.$message.success(this.editObj.id ? "修改成功" : "新增成功");
        this.addShow = false;
        this.editObj = { editIdx: -1 };
        this.getData();
      });
    },
    getData() {
      this.tloading = true;
      getProjectAlbumList(this.id).then((res) => {
        this.tloading = false;
        this.tableData = this.projectid != 0 ? res.data.filter((x) => x.projectId == this.projectid) : res.data;
      });
    },
  },
};
</script>
<style scoped lang="scss">
.lookAlbum {
  ::v-deep .el-dialog__body {
    padding-top: 10px !important;
  }
}
.avatar-uploader,
.el-upload {
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px !important;
  height: 178px;
  display: inline-block;
}

.avatar-uploader:hover,
.el-upload:hover {
  border-color: #409eff;
}

.upload-text {
  font-size: 14px;
  color: #8c939d;
  line-height: 178px;
  text-align: center;
  margin: 0 auto;
}

.avatar {
  width: 178px;
  height: 178px;
  display: inline-block;
  object-fit: cover;
}
.btn {
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.5);
  color: #fff;
  width: 178px !important;
  height: 178px;
  border-radius: 0 6px 0 6px;
  text-align: center;
  line-height: 178px;
}

.avatar-uploader:hover {
  // 鼠标悬浮显示按钮
  .btn {
    display: block;
  }
}
</style>
